package com.greenantmall.view;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.greenantmall.R;

public class PopMenu {
	private ArrayList<String> itemList;
	private ArrayList<Integer> imageList;
	private Context context;
	private PopupWindow popupWindow ;
	private ListView listView;
	private OnItemClickListener listener;
	

	public PopMenu(Context context) {
		this.context = context;

		itemList = new ArrayList<String>(5);
		imageList=new ArrayList<Integer>();
		
		View view = LayoutInflater.from(context).inflate(R.layout.popmenu, null);
        
        listView = (ListView)view.findViewById(R.id.listView);
        listView.setAdapter(new PopAdapter());
        listView.setFocusableInTouchMode(true);
        listView.setFocusable(true);
        
        popupWindow = new PopupWindow(view, 
        		context.getResources().getDimensionPixelSize(R.dimen.dimen_200), 
        		LayoutParams.WRAP_CONTENT);
        
        // �����Ϊ�˵��������Back��Ҳ��ʹ����ʧ�����Ҳ�����Ӱ����ı�����������ģ�
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
	}

	//���ò˵�����������
	public void setOnItemClickListener(OnItemClickListener listener) {
		this.listener = listener;
//		listener.onItemClick(arg0, arg1, arg2, arg3)
//		listView.setOnItemClickListener(listener);
		
		listView.setOnItemClickListener(listener);
	}

	//������Ӳ˵���
	public void addItems(String[] items,int[] drawables) {
		for (String s : items)
			itemList.add(s);
		
		for(int i: drawables)
			imageList.add(i);
	}

	//������Ӳ˵���
	public void addItem(String item,int drawable) {
		itemList.add(item);
		imageList.add(drawable);
	}

	public void showAsDropDown(View parent) {
		popupWindow.showAsDropDown(parent, 10, 
				context.getResources().getDimensionPixelSize(R.dimen.dimen_5));
		
		// ʹ��ۼ�
        popupWindow.setFocusable(true);
        // ����������������ʧ
        popupWindow.setOutsideTouchable(true);
        //ˢ��״̬
        popupWindow.update();
	}
	
	//���ز˵�
	public void dismiss() {
		popupWindow.dismiss();
	}

	// ������
	private final class PopAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return itemList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return itemList.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder;
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(R.layout.pomenu_item, null);
				holder = new ViewHolder();
				holder.groupItem = (TextView) convertView.findViewById(R.id.textView);
				holder.imageItem = (ImageView) convertView.findViewById(R.id.imageView);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.groupItem.setText(itemList.get(position));
			if(imageList!=null&&imageList.size()>position)
			{
				holder.imageItem.setBackgroundResource(imageList.get(position));
			}
			
			convertView.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					if(listener!=null)
					{
						listener.onItemClick(null, null, position, position);
					}
					//popupWindow.dismiss();
				}
			});
			holder.groupItem.setOnClickListener(new View.OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    if(listener!=null)
                    {
                        listener.onItemClick(null, null, position, position);
                    }
                   // popupWindow.dismiss();
                }
            });
			 holder.imageItem.setOnClickListener(new View.OnClickListener() {
	                
	                @Override
	                public void onClick(View arg0) {
	                    if(listener!=null)
	                    {
	                        listener.onItemClick(null, null, position, position);
	                    }
	                 //   popupWindow.dismiss();
	                }
	            });
		
			return convertView;
		}

		private final class ViewHolder {
			TextView groupItem;
			ImageView imageItem;
		}
	}
}
